from django.contrib import admin
from .models import Question, Choice

class ChoiceInline(admin.TabularInline):
    """内联编辑 Choice 模型"""
    model = Choice
    extra = 3  # 默认显示3个空白选项
    fields = ['choice_text', 'votes']
    readonly_fields = ['votes']  # 投票数设为只读
    classes = ['collapse']  # 默认折叠，可选

@admin.register(Question)
class QuestionAdmin(admin.ModelAdmin):
    """Question 模型的管理界面"""
    
    # 字段分组布局
    fieldsets = [
        (None, {
            "fields": ["question_text"]
        }),
        ("日期信息", {
            "fields": ["pub_date"],
            "classes": ["collapse"]  # 可折叠区域
        }),
    ]
    
    # 内联编辑关联的 Choice
    inlines = [ChoiceInline]
    
    # 列表页显示配置
    list_display = ["question_text", "pub_date", "was_published_recently"]
    
    # 列表页筛选器
    list_filter = ["pub_date"]
    
    # 搜索功能
    search_fields = ["question_text"]
    
    # 日期层次导航
    date_hierarchy = "pub_date"
    
    # 每页显示数量
    list_per_page = 20
    
    # 排序
    ordering = ["-pub_date"]